package handler;

import entity.CourseDO;
import entity.GradeDO;
import service.CourseService;
import service.GradeService;
import service.impl.CourseServiceImpI;
import service.impl.GradeServiceImpI;
import view.AddCourseView;
import view.AddGradeView;
import view.CourseView;
import view.GradeView;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AddGradeViewHandler implements ActionListener {
    private AddGradeView addGradeView;

    //private CourseView courseView;
    private GradeView gradeView;

    public AddGradeViewHandler(AddGradeView addGradeView, GradeView gradeView) {
        this.addGradeView = addGradeView;
        this.gradeView = gradeView;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton = (JButton) e.getSource();
        String text = jButton.getText();
        if ("添加".equals(text)) {
            GradeService gradeService = new GradeServiceImpI();
            GradeDO gradeDO = addGradeView.buildGradeDO();
            boolean addResult = gradeService.add(gradeDO);
            if (addResult) {
                //重新加载表格
                gradeView.reloadTable();
                addGradeView.dispose();
            } else {
                JOptionPane.showMessageDialog(addGradeView, "添加失败");
            }
        }
    }
}
